package com.lingjtx.common.captcha;

import com.lingjtx.common.core.RedisOptions;

import java.awt.*;
import java.nio.charset.StandardCharsets;

public interface CaptchaHandle {

    int HAN_ZI_SIZE = 25;

    int HAN_ZI_SIZE_HALF = HAN_ZI_SIZE / 2;
    //check校验坐标
    String REDIS_CAPTCHA_KEY = "running:captcha:%s";

    //后台二次校验坐标
    String REDIS_SECOND_CAPTCHA_KEY = "running:captcha:second-%s";

    Long EXP_SECONDS = 2 * 60L;

    Long EXPIRESIN_THREE = 3 * 60L;

    String waterMark = "John Liu";

    int slipOffset = 5;

    boolean captchaInterferenceOptions = true; // 是否开启验证码干扰选项：true:开启

    CaptchaVo generator(RedisOptions redis);

    String valid(String captchaUid, String pointJson, RedisOptions redis);

    default Font getWaterMarkFont() {
        return new Font("华文细黑", Font.PLAIN, HAN_ZI_SIZE / 2);
    }

    default Font getClickWordFont() {
        return new Font("微软雅黑", Font.BOLD, HAN_ZI_SIZE);
    }

    default int getEnOrChLength(String waterMark) {
        int enCount = 0;
        int chCount = 0;
        for (int i = 0; i < waterMark.length(); i++) {
            int length = String.valueOf(waterMark.charAt(i)).getBytes(StandardCharsets.UTF_8).length;
            if (length > 1) {
                chCount++;
            } else {
                enCount++;
            }
        }
        int chOffset = (HAN_ZI_SIZE / 2) * chCount + 5;
        int enOffset = enCount * 8;
        return chOffset + enOffset;
    }
}
